// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// C [author] rhyme_lph
// m [email]  rhymelph@gmail.com
// Z [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/country_entity.dart';
import 'package:finance_app/src/entities/martial_status_entity.dart';
import 'package:finance_app/src/entities/pass_type_entity.dart';
import 'package:finance_app/src/page/hr_payroll/employee/new_employee_page.dart';
import 'package:finance_app/src/provider/dashboard/expense/new_expense_provider.dart';
import 'package:finance_app/src/provider/hr_payroll/employee/views/employee_provider.dart';
import 'package:finance_app/src/widgets/bottom_sheet/country_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/date_time_select_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/list_select_bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/page/hr_payroll/employee/views/particulars_view.dart';

class ParticularsProvider extends BaseProvider {
  TextEditingController employeeIdController = TextEditingController();
  FocusNode employeeIdFocusNode = FocusNode();

  TextEditingController nameController = TextEditingController();
  FocusNode nameFocusNode = FocusNode();

  TextEditingController nricController = TextEditingController();
  FocusNode nricFocusNode = FocusNode();

  DateTime birthDate;

  String gender;

  String race;

  MartialStatusEntity maritalStatus;

  PassTypeEntity passType;

  CountryEntity nationality;

  DateTime passIssueDate;

  DateTime passExpiryDate;

  bool get isShowPrStart => passType?.key == 'spr';
  DateTime prStart;

  @override
  Future get dataFuture => null;

  @override
  void dispose() {
    super.dispose();
    employeeIdController.dispose();
    employeeIdFocusNode.dispose();
    nameController.dispose();
    nameFocusNode.dispose();
    nricController.dispose();
    nricFocusNode.dispose();
  }

  void unFocus() {
    obtainContext?.call((context) {
      FocusScope.of(context).requestFocus(FocusNode());
    });
  }

  void onChangeBirth() {
    unFocus();
    obtainContext?.call((context) async {
      DateTime result = await showDateTimeSelectBottomSheet(
          context, S.current.date_of_birth, birthDate,
          withinFinancialYear: false);
      if (result != null) {
        birthDate = result;
        update();
      }
    });
  }

  void onChangeGender() {
    unFocus();
    obtainContext?.call((context) async {
      gender = await showGenderSelectBottomSheet(context, gender);
      update();
    });
  }

  void onChangeRace() {
    unFocus();
    obtainContext?.call((context) async {
      race = await showRaceSelectBottomSheet(context, race);
      if (race == null) {
        update();
        return;
      }
      final pro = getParentProvider<NewEmployeeProvider>()
          ?.getChildProvider<EmployeeProvider>();
      print(race);
      if (pro != null) {
        pro.shgAmount = 0.0;
        if (race == 'Chinese') {
          pro.selfHelpGroup = Config.settingData.contribution.first;
        } else if (race == 'Malay') {
          pro.selfHelpGroup = Config.settingData.contribution[1];
        } else if (race == 'Indian') {
          pro.selfHelpGroup = Config.settingData.contribution[2];
        } else if (race == 'Others') {
          pro.selfHelpGroup = Config.settingData.contribution[3];
        }
        pro.update();
      }
      update();
    });
  }

  void onChangeMaritalStatus() {
    unFocus();
    obtainContext?.call((context) async {
      maritalStatus =
          await showMaritalStatusSelectBottomSheet(context, maritalStatus);
      update();
    });
  }

  void onChangePassType() {
    unFocus();
    obtainContext?.call((context) async {
      passType = await showPassTypeSelectBottomSheet(context, passType);
      if (passType == null) return;
      if (passType.key == 'singaporean') {
        nationality = Config.defaultCountry;
      }
      // spr show pr start
      // sp  wp ,ignore contribution

      print(passType.key);
      update();
    });
  }

  void onChangeNationality() {
    unFocus();
    obtainContext?.call((context) async {
      nationality = await showCountryBottomSheet(context, nationality);
      update();
    });
  }

  void onChangePassIssueDate() {
    unFocus();
    obtainContext?.call((context) async {
      DateTime result = await showDateTimeSelectBottomSheet(
          context, S.current.pass_issue_date, passIssueDate,
          withinFinancialYear: false);
      if (result != null) {
        passIssueDate = result;
        update();
      }
    });
  }

  void onChangePassExpiryDate() {
    unFocus();
    obtainContext?.call((context) async {
      DateTime result = await showDateTimeSelectBottomSheet(
          context, S.current.pass_expiry_date, passExpiryDate,
          withinFinancialYear: false);
      if (result != null) {
        passExpiryDate = result;
        update();
      }
    });
  }

  void onChangePrStartDate() {
    unFocus();
    obtainContext?.call((context) async {
      DateTime result = await showDateTimeSelectBottomSheet(
          context, S.current.pr_start_date, prStart,
          withinFinancialYear: false);
      if (result != null) {
        prStart = result;
        update();
      }
    });
  }
}
